<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="closeDialog"
    @open="getRoleList"
  >
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="checkList" v-loading="loading">
      <el-checkbox v-for="item in arrRoleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
      <!-- <el-checkbox label="002">普通员工</el-checkbox>
      <el-checkbox label="003">临时工</el-checkbox> -->
    </el-checkbox-group>

    <template #footer>
      <div style="text-align: right">
        <el-button type="primary" @click="submitUserRole()">确定</el-button>
        <el-button @click="closeDialog()">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleListApi } from '@/api/setting'
import { getStaffInfoApi } from '@/api/user'
import { editAssignRolesApi } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      checkList: [], // 收集复选框中的值
      arrRoleList: [], // 所有角色列表,
      loading: false
    }
  },

  methods: {
    closeDialog() {
      this.$emit('update:showRoleDialog', false)
    }, // 获取所有角色列表
    async getRoleList() {
      // 打开弹层清空一下避免缓存
      this.checkList = []
      this.loading = true
      const { data } = await getRoleListApi(1, 10000)
      this.arrRoleList = data.rows
      // 获取当前员工的角色
      const res = await getStaffInfoApi(this.userId)
      // 判断当前的员工角色是不是空的
      this.checkList = res.data.roleIds || []
      this.loading = false
    }, // 提交用户角色
    async submitUserRole() {
      await editAssignRolesApi(this.userId, this.checkList)
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>
